package com.lagou.edu.servce.impl;

import com.lagou.edu.dao.UserDao;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.servce.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

/**
 * @Author 梅松
 * @Date 2021/10/18 23:00
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;



    /**
     * 查询用户是否已经注册
     * @param email
     * @return
     */
    @Override
    public boolean isRegistered(String email){
        LagouUser user = userDao.findLagouUserByEmail(email);
        return user!=null?true:false;
    }

    /**
     * 保存用户
     * @param lagouUser
     * @return
     */
    @Override
    public LagouUser saveUser(LagouUser lagouUser){
        lagouUser = userDao.save(lagouUser);
        return lagouUser;
    }

    /**
     * 登录方法
     * @param email
     * @param password
     * @return
     */
    @Override
    public boolean login(String email,String password){
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        Example<LagouUser> example = Example.of(lagouUser);
        lagouUser = userDao.findOne(example).orElse(null);
        return lagouUser!=null?true:false;
    }
}
